<template>
  <el-dialog title="分配订单" :model-value="dialog" @close="close">
    <el-form :model="formData" :rules="rules" ref="uploadForm" label-width="auto" style="padding: 0 20px" class="Dform">
      <el-form-item label="调度企业：" prop="useCarGroup">
        <el-select v-model="formData.useCarGroup" filterable clearable style="width: 100%" placeholder="请选择调度企业">
          <el-option v-for="item in list" :key="item.partnerUuid" :label="item.partnerName" :value="item.partnerUuid" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit()">提交</el-button>
        <el-button v-if="!formData.orderUuid" @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, ref, watch, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { queryDispatchList, allocateOrder } from "@/api/order"
import { orderStatusMap, orderTypeMap } from '@/config/constants'

export default defineComponent({
  props: {
    dialog: {
      type: Boolean,
      default: false,
    },
    currentForm: {
      type: Object,
      default: () => { },
    }
  },
  emits: ['update:currentForm', 'update:dialog', 'refresh'],
  setup(props, context) {
    const formData = reactive(props.currentForm)
    const uploadForm = ref<any>(null)

    const list = ref<any>([])
    watch(() => props.dialog,
      (val) => {
        console.log(props.currentForm, val)
        Object.assign(formData, props.currentForm)
        console.log(formData)
      }
    )

    const close = () => {
      context.emit('update:dialog', false)
      context.emit('refresh', { ...formData })
      context.emit('update:currentForm', {})
    }

    onActivated(() => {
      console.log('onActivated')
    })



    onMounted(() => {
      queryDispatchList({ pageNum: 1, pageSize: 1000 }).then((res: any) => {
        list.value = res.data || []
      })

    })

    const onSubmit = () => {
      uploadForm.value.validate((valid: boolean) => {
        if (valid) {
          allocateOrder(formData).then((res: any) => {
            if (res.code == 200) {
              ElMessage.success('提交成功')
              close()
            }
          })
        } else {
          ElMessage({
            message: '验证失败',
            type: 'error',
            duration: 1000,
          })
          return false
        }
      })
    }

    const resetForm = () => {
      uploadForm.value.resetFields()
      context.emit('update:currentForm', {})
    }

    return {
      uploadForm,
      formData,
      list,
      orderStatusMap,
      orderTypeMap,
      close,
      resetForm,
      onSubmit,
      rules: {
        driverUuid: [
          { required: true, message: '请选择司机', trigger: 'blur' }
        ],
      },
    }
  },
})
</script>

<style></style>
